"""
简单的迁移测试 - 仅测试数据读取不依赖数据库
"""

import json
import tempfile
import os
from pathlib import Path

def test_simple_migration():
    """测试迁移脚本的基本数据处理功能"""
    print("=== 简单迁移测试 ===")
    
    # 创建临时测试数据
    with tempfile.TemporaryDirectory() as temp_dir:
        temp_path = Path(temp_dir)
        
        # 创建测试数据
        sessions_data = {
            "session1": {
                "title": "测试会话1",
                "role_id": "emma",
                "model": "deepseek-v3-1-250821", 
                "messages": [
                    {"role": "user", "content": "Hello"},
                    {"role": "assistant", "content": "Hi there!"}
                ]
            }
        }
        
        roles_data = {
            "role1": {
                "name": "测试程序员",
                "title": "高级工程师",
                "input_spec": "编程问题",
                "output_spec": "代码解答",
                "custom_traits": [{"key": "背景", "value": "10年经验"}],
                "system_prompt_snapshot": "你是专业程序员"
            }
        }
        
        # 写入文件
        sessions_file = temp_path / "sessions.json"
        roles_file = temp_path / "custom_roles.json"
        
        with open(sessions_file, 'w', encoding='utf-8') as f:
            json.dump(sessions_data, f, ensure_ascii=False, indent=2)
            
        with open(roles_file, 'w', encoding='utf-8') as f:
            json.dump(roles_data, f, ensure_ascii=False, indent=2)
        
        print(f"创建测试数据到: {temp_dir}")
        print(f"  sessions.json: {len(sessions_data)} 个会话")
        print(f"  custom_roles.json: {len(roles_data)} 个角色")
        
        # 测试数据读取
        print("\n测试数据读取...")
        
        # 读取角色数据
        with open(roles_file, 'r', encoding='utf-8') as f:
            read_roles = json.load(f)
        print(f"成功读取角色: {len(read_roles)} 个")
        
        # 读取会话数据  
        with open(sessions_file, 'r', encoding='utf-8') as f:
            read_sessions = json.load(f)
        print(f"成功读取会话: {len(read_sessions)} 个")
        
        # 计算消息数量
        total_messages = sum(len(session.get('messages', [])) for session in read_sessions.values())
        print(f"总消息数量: {total_messages} 条")
        
        # 验证数据结构
        print("\n验证数据结构...")
        for role_id, role_data in read_roles.items():
            if not all(key in role_data for key in ['name', 'title', 'input_spec', 'output_spec']):
                print(f"角色 {role_id} 缺少必需字段")
                return False
            print(f"角色 {role_id}: {role_data['name']} - {role_data['title']}")
                
        for session_id, session_data in read_sessions.items():
            if not all(key in session_data for key in ['title', 'model']):
                print(f"会话 {session_id} 缺少必需字段")
                return False
            print(f"会话 {session_id}: {session_data['title']}")
            
        print("\n测试通过！迁移脚本的数据处理功能正常")
        return True

if __name__ == "__main__":
    success = test_simple_migration()
    if success:
        print("\n[SUCCESS] 所有测试通过")
        print("下一步:")
        print("1. 设置Supabase环境变量")  
        print("2. 在Supabase中执行init.sql")
        print("3. 运行完整迁移测试")
    else:
        print("\n[ERROR] 测试失败")